package com.yanqu.road.entity.activity.tonbing.join;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.activity.camp.CampUnion;
import com.yanqu.road.entity.activity.camp.CampUnionMember;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_tonbing_union_join")
public class CrossTonBingUnionJoin extends DataObject implements CampUnion {

    @FieldTypeAnnotation(desc = "活动ID", select = true, update = true)
    private int activityId;
    @FieldTypeAnnotation(desc = "商会UID", update = true)
    private String unionUid;
    @FieldTypeAnnotation(desc = "商会信息")
    private UnionBaseInfo unionBaseInfo;
    @FieldTypeAnnotation(desc = "商会成员")
    private Map<Long, CrossTonBingUnionJoinMember> memberMap = new ConcurrentHashMap<>();
    @FieldTypeAnnotation(desc = "阵营ID")
    private int campId;
    @FieldTypeAnnotation(desc = "商会总赚钱")
    private long totalAbility = 0L;
    @FieldTypeAnnotation(desc = "压缩后赚钱")
    private BigDecimal compressedAbility = BigDecimal.ZERO;
    @FieldTypeAnnotation(desc = "总赚钱排名")
    private int oldRank;
    @FieldTypeAnnotation(desc = "压缩赚钱排名")
    private int compressRank;

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public String getUnionUid() {
        return unionUid;
    }

    public void setUnionUid(String unionUid) {
        if (unionUid == null) {
            return;
        }
        if (this.unionUid == null || !this.unionUid.equals(unionUid)) {
            this.unionUid = unionUid;
            setOp(DataStatus.Update);
        }
    }

    public UnionBaseInfo getUnionBaseInfo() {
        return unionBaseInfo;
    }

    public void setUnionBaseInfo(UnionBaseInfo unionBaseInfo) {
        if (null == unionBaseInfo) {
            return;
        }
        if (this.unionBaseInfo == null || !this.unionBaseInfo.equals(unionBaseInfo)) {
            this.unionBaseInfo = unionBaseInfo;
            setOp(DataStatus.Update);
        }
    }

    public Map<Long, CrossTonBingUnionJoinMember> getMemberMap() {
        return memberMap;
    }

    public void setMemberMap(Map<Long, CrossTonBingUnionJoinMember> memberMap) {
        if (null == memberMap) {
            return;
        }
        this.memberMap = memberMap;
        setOp(DataStatus.Update);
    }

    public int getCampId() {
        return campId;
    }

    public void setCampId(int campId) {
        if (this.campId != campId) {
            this.campId = campId;
            setOp(DataStatus.Update);
        }
    }

    // ==============================

    public long getTotalAbility() {
        if (totalAbility == 0) {
            reCalcAbility();
        }
        return totalAbility;
    }

    public void setTotalAbility(long totalAbility) {
        if (totalAbility != this.totalAbility) {
            this.totalAbility = totalAbility;
            setUpdateOption();
        }
    }

    // ==============================

    public void reCalcAbility() {
        long totalAbility = getMemberMap().values().stream().mapToLong(CrossTonBingUnionJoinMember::getTopAbility).sum();
        setTotalAbility(totalAbility);
    }

    public BigDecimal getCompressedAbility() {
        return compressedAbility;
    }

    public double getCompressedAbilityDouble() {
        return compressedAbility.doubleValue();
    }

    public void setCompressedAbility(BigDecimal compressedAbility) {
        this.compressedAbility = compressedAbility;
        setUpdateOption();
    }

    public int getOldRank() {
        return oldRank;
    }

    public void setOldRank(int oldRank) {
        this.oldRank = oldRank;
        setUpdateOption();
    }

    public int getCompressRank() {
        return compressRank;
    }

    public void setCompressRank(int compressRank) {
        this.compressRank = compressRank;
        setUpdateOption();
    }

    @Override
    public Map<Long, CampUnionMember> getCampMemberMap() {
        return new HashMap<>(memberMap);
    }
}
